import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { environment } from '@env/environment';
import { STChange, STClickRowClassNameType, STColumn, STComponent, STPage } from '@delon/abc/st';
import { DragModalService } from '../../../../../../shared/utils/drag-modal.service';
import { AuthDictService } from '../../../../../system/dict/service/dict.service';
import { BdOperationProcessEditComponent } from '../../../../basic-data/operation-process/edit/edit.component';
import { deepCopy } from '@delon/util';
import { TallyCheckService } from '../../tally-check.service';
import { TallyCheckOstEditComponent } from '../edit/edit.component';
import moment from 'moment';
import { SearchParamUtils } from '../../../../../../shared/utils/SearchParamUtils';

@Component({
  selector: 'app-ost-tally-check-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.less']
})
export class TallyCheckOstListComponent implements OnInit {
	@Input()
	searchParams: any;
	pageInfo = {
		total: 0,
		current: 1,
		size: 20,
		loading: false,
		nzPageSizeOptions: environment.nzPageSizeOptions
	};
	pageSetting: STPage = {
		total: '',
		show: true,
		showSize: true,
		front: false,
		pageSizes: environment.nzPageSizeOptions
	};
	@ViewChild('st', { static: false }) private st!: STComponent;
	columns: STColumn[] = [
		{
			title: '序号',
			width: 50,
			index: 'index',
			type: 'no',
			noIndex: (item, col, index) => index + 1 + (this.st.pi - 1) * this.st.ps,
			fixed: 'left',
			className: 'tc'
		},
		{
			title: '检算状态',
			index: 'isCheck',
			render: 'isCheck',
			width: 60
		},
		{
			title: '理货时间',
			index: 'timeInfo',
			width: 100
		},
		{
			title: '理货员',
			index: 'createUserName',
			width: 50
		},
		{
			title: '船信息',
			index: 'shipInfo',
			width: 100
		},
		{
			title: '单号',
			index: 'billNo',
			width: 90
		},
		{
			title: '舱单货名',
			index: 'billCargoTypeName',
			width: 60
		},
		{
			title: '理货包装',
			index: 'tallyPackageTypeName',
			width: 60
		},
		{
			title: '航陆运',
			index: 'shipLandName',
			width: 60
		},
		{
			title: '理货类型',
			index: 'tallyTypeName',
			width: 60
		},
		{
			title: '作业过程',
			index: 'operationTypeName',
			width: 60
		},
		{
			title: '货垛',
			index: 'positionInfo',
			width: 80
		},
		{
			title: '理货件吨',
			index: 'tallyPieceTon',
			format: (item) => item.tallyPiece + '/' + item.tallyTon,
			width: 60
		},
		{
			title: '装卸队组',
			index: 'workerInfo',
			width: 80
		},
		{
			title: '作业机械',
			index: 'machineInfo',
			width: 100
		},
		{
			title: '安全生产',
			index: 'isSafeProduction',
			render: 'isSafeProduction',
			width: 60
		},
		{
			title: '备注',
			index: 'remark',
			width: 80
		},
		{
			title: '操作',
			className: 'tc',
			width: 60,
			fixed: 'right',
			render: 'manage'
		}
	];
	dataList = [];
	constructor(
		private modalService: DragModalService,
		private tallyCheckService: TallyCheckService
	) {
	}

	ngOnInit(): void {
		this.loadData();
	}

	loadData(): any {
		const params = {
			...this.searchParams,
			size: this.pageInfo.size,
			current: this.pageInfo.current
		};
		this.pageInfo.loading = true;
		this.tallyCheckService.ostTallyCheckPage(params).subscribe((res: any) => {
			this.dataList = res.records;
			this.pageInfo.total = res.total;
			this.pageSetting.total = res.total;
			this.pageInfo.loading = false;
		});
	}

	resetParams(): void {
		this.pageInfo.current = 1;
		for (let key of Object.keys(this.searchParams)) {
			this.searchParams[key] = "";
		}
	}

	stChange(ret: STChange) {
		if (ret.type === 'pi' || ret.type === 'ps') {
			this.pageInfo.current = ret.pi;
			this.pageInfo.size = ret.ps;
			this.loadData();
		}
	}

	sa: STClickRowClassNameType = {
		fn(record: any, index: number): string {
			return 'selected-row';
		},
		exclusive: true
	};

	changeIsCheck(item: any) {
		this.tallyCheckService.ostChangeIsCheck(item.id).subscribe(res => {
			this.st.setRow(item, { isCheck: res });
		});
	}

	correct(item: any) {
		this.modalService
			.createDrag({
				nzComponentParams: { record: item },
				nzWidth: '800px',
				nzContent: TallyCheckOstEditComponent,
				nzTitle: '修正',
				nzFooter: null
			})
			.afterClose.subscribe(res => {
			this.loadData();
		});
	}
}
